import {
  indexApi
} from '../../../api/index';
import {
  request
} from '../../../utils/request';
import Dialog from '@vant/weapp/dialog/dialog';
Page({
  data: {
    defaultData: {
      title: "申请成为师傅", // 导航栏标题
      imageSrc: "/images/back.png",
      isBackToPreviousPage: true
    },
    userName:"",
    workAge:null,
    age:null,
    skills: [],
    skillsNames: [],
    selectedSkillName: '请选择技能',
    selectedSkillId: null
  },

  onLoad(options) {
    this.getskills()
  },


  // 技能
  async getskills() {
    try {
      const res = await request({
        url: indexApi.getSkillList(),
        data: {}
      });
      console.log(res);
      const skills = res.data;
      const skillsNames = skills.map(skill => skill.skillName);
      this.setData({
        skills: skills,
        skillsNames: skillsNames
      });
    } catch (error) {
      console.error("Error:", error);
    }
  },
  onPickerChange: function(event) {
    console.log("event",event);
    const index = event.detail.value;
    const selectedSkill = this.data.skills[index];
    this.setData({
      selectedSkillName: selectedSkill.skillName,
      selectedSkillId: selectedSkill.skillId
    });
    console.log('Selected Skill ID:', this.data.selectedSkillId);
  },

  // 申请成为师傅
  async clickBecomeWorker() {
    const reqTemplate={
      userName:this.data.userName,
      workAge:this.data.workAge,
      age:this.data.age,
      skillsId:this.data.selectedSkillId
    }
    try {
      const res = await request({
        url: indexApi.becomeWorker(),
        method:"POST",
        data: reqTemplate
      });
      console.log(res);
      Dialog.alert({
        title: res.message,
      }).then(() => {
        wx.switchTab({
          url: '/pages/home/index',
        })
      });
     
    } catch (error) {
      console.error("Error:", error);
    }
  },

  //姓名
  onUserNameInput(event){
    console.log("event.detail",event.detail);
    this.setData({
      userName:event.detail
    })
  },
  onAgeInput(event){
    console.log("event.detail",event.detail);
    this.setData({
      age:event.detail
    })
  },
  onWorkAgeInput(event){
    console.log("event.detail",event.detail);
    this.setData({
      workAge:event.detail
    })
  },

  toBack() {
    wx.navigateBack({
      delta: 1
    });
  },
 
  

})